package com.unionftech.common.permissions.tip.impl;

import android.app.Activity;
import android.content.Context;

import androidx.fragment.app.FragmentActivity;

import com.unionftech.common.permissions.tip.IAction;
import com.unionftech.common.permissions.tip.IPermissionTipInfoAdapter;
import com.unionftech.common.permissions.tip.ITipShowController;
import com.unionftech.common.permissions.tip.bean.DialogStyleInfo;
import com.unionftech.common.permissions.tip.bean.PermissionTipInfo;
import com.unionftech.common.permissions.tip.impl.view.PermissionGotoSettingDialog;
import com.unionftech.common.permissions.tip.impl.view.PermissionSceneDialog;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class DefaultTipsShowController implements ITipShowController {
    private static final String PERMISSION_TIPS_DIALOG = "PermissionTipsDialog";
    private DialogStyleInfo dialogStyleInfo;
    private IPermissionTipInfoAdapter tipInfoAdapter;

    public DefaultTipsShowController(IPermissionTipInfoAdapter tipInfoAdapter) {
        this.tipInfoAdapter = tipInfoAdapter;
        this.dialogStyleInfo = new DialogStyleInfo();
    }

    public DefaultTipsShowController(IPermissionTipInfoAdapter tipInfoAdapter, DialogStyleInfo dialogStyleInfo) {
        this.tipInfoAdapter = tipInfoAdapter;
        this.dialogStyleInfo = dialogStyleInfo;
    }

    @Override
    public void showTipDialog(Activity activity, String[] permissions, IAction action) {
        FragmentActivity fragmentActivity = null;
        if (activity instanceof FragmentActivity) {
            fragmentActivity = (FragmentActivity) activity;
        }
        if (fragmentActivity == null) {
            action.onComplete();
            return;
        }
        int total = permissions.length;
        AtomicInteger count = new AtomicInteger();
        for (String permission : permissions) {
            // 展示权限说明弹窗
            String[] strings = {permission};
            List<PermissionTipInfo> tipInfoList = getTipInfoList(activity, strings);
            if (!tipInfoList.isEmpty()) {
                PermissionSceneDialog dialog = new PermissionSceneDialog(fragmentActivity, tipInfoList);
                dialog.setCancelable(false);
                dialog.setDismissListener(() -> {
                    count.getAndIncrement();
                    if (count.get() == total) {
                        action.onComplete();
                    }
                });
                dialog.show(fragmentActivity.getSupportFragmentManager(), PERMISSION_TIPS_DIALOG + "i");
            } else {
                count.getAndIncrement();
                if (count.get() == total) {
                    action.onComplete();
                }
            }
        }
    }

    @Override
    public void showGotoSettingDialog(Activity activity, String title, String message, IAction action) {
        // 展示前往设置弹窗
        FragmentActivity fragmentActivity = null;
        if (activity instanceof FragmentActivity) {
            fragmentActivity = (FragmentActivity) activity;
        }
        if (fragmentActivity != null) {
            PermissionGotoSettingDialog dialog = new PermissionGotoSettingDialog(fragmentActivity);
            dialog.setTitle(title);
            dialog.setMessage(message);
            dialog.setCancelable(false);
            dialog.setListener(action::onComplete);
            dialog.show(fragmentActivity.getSupportFragmentManager(), "PermissionGotoSettingsDialog");
            return;
        }
        action.onComplete();
    }

    private List<PermissionTipInfo> getTipInfoList(Context context, String[] permissions) {
        LinkedHashSet<PermissionTipInfo> set = new LinkedHashSet<>();
        for (String permission : permissions) {
            PermissionTipInfo tipInfo = tipInfoAdapter.getPermissionTipInfo(context, permission);
            if (tipInfo != null) {
                set.add(tipInfo);
            }
        }
        return new ArrayList<>(set);
    }
}
